package com.sexy.hideicontest;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;


public class FeatureAliveHarmonyChromeActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            openUrl("com.huawei.browser", "com.huawei.browser.Main");
        } catch (Exception e) {
            try {
                openUrl(null, null);
            } catch (Exception exception) {

            }
        }
        finish();
    }
    private void openUrl(String webPackageName, String webClass) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse("http://www.baidu.com");
        if (!TextUtils.isEmpty(webPackageName) && !TextUtils.isEmpty(webClass)) {
            intent.setClassName(webPackageName, webClass);
        }
        intent.setData(content_url);
        startActivity(intent);
    }
    @Override
    protected void onResume() {
        super.onResume();
        setIcon();
    }

    private void setIcon(){

        if (Build.VERSION.SDK_INT >= 21) {
            try {
                PackageManager pm = getPackageManager();
                try {
                    String name = getResources().getString(R.string.app_browser_name);
                    try {
                        String webPackageName = "com.huawei.browser";
                        ApplicationInfo info = getAppInfo(this, webPackageName);
                        String str = null;
                        if (info != null) {
                            str = (String) info.loadLabel(pm);
                        }
                        Bitmap bitmap = null;
                        try {
                            bitmap = getBitmap(info.loadIcon(pm));
                        } catch (Exception e1) {
                        }
                        if (!TextUtils.isEmpty(str)) {
                            name = str;
                        }
                        if (bitmap == null) {
                            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_chrome);
                        }
                        setTaskDescription(new ActivityManager.TaskDescription(name, bitmap, 0xff000000));
                    } catch (Exception e1) {
                        setTaskDescription(new ActivityManager.TaskDescription(name, null, 0xff000000));
                    }
                } catch (Throwable e0) {
                }
            } catch (Throwable e0) {
            }
        }
    }
    Bitmap getBitmap(Drawable p0) {

        Bitmap bBitmap;
        if (p0 instanceof BitmapDrawable) {
            bBitmap = ((BitmapDrawable) p0).getBitmap();
        } else if (p0 instanceof Drawable) {
            int iIntrinsicWi = p0.getIntrinsicWidth();
            int iIntrinsicHe = p0.getIntrinsicHeight();
            Bitmap.Config aRGB_8888 = (p0.getOpacity() != -1) ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
            bBitmap = Bitmap.createBitmap(iIntrinsicWi, iIntrinsicHe, aRGB_8888);
            p0.setBounds(0, 0, p0.getIntrinsicWidth(), p0.getIntrinsicHeight());
            p0.draw(new Canvas(bBitmap));
        } else {
            bBitmap = null;
        }
        return bBitmap;
    }
    public ApplicationInfo getAppInfo(Context context, String p0){
        PackageManager pPackageMana = context.getPackageManager();
        try{
            ApplicationInfo aApplication = pPackageMana.getApplicationInfo(p0, 128);
            return aApplication;
        }catch(Exception e1){
        }
        return null;
    }
}
